package com.waitAndnotifyAll;


/**
 * @Author weijingchen
 * @Date 2025/6/23 9:19
 * @Version 1.0.0
 */
public class Foodle extends Thread {
    @Override
    public void run() {
        /*
         * 1. 循环
         * 2。同步代码块
         * 3。判断共享数据是否到末尾（到了末尾）
         * 4.判断共享数据是否到末尾（没有到，执行核心逻辑）
         *
         * */

        while (true) {
            synchronized (Desk.lock) {
                if (Desk.count == 0) {
                    break;
                } else {
                    if (Desk.foodFlag == 0) {
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        System.out.println("吃货正在吃：" + Desk.count + "碗");
                        Desk.count--;
                        System.out.println("吃货还能吃：" + Desk.count + "碗");
                        Desk.lock.notifyAll();
                        Desk.foodFlag = 0;
                    }
                }
            }
        }

    }
}
